<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><? echo $title ?></title>
    <? require("lib.php"); ?>
    <link rel="stylesheet" href="css/jquery.datetimepicker.css" type="text/css" media="screen" />
    <script type="text/javascript" src="js/jquery.datetimepicker.js"></script>
    <style>
        .tag_add, .tag_cancel{clear:both; display:block; width:96%; height:100%; border:1px solid #cccccc; padding-top:15px; padding-left: 10px; padding-right: 10px; text-align:left; text-decoration:none}
        a.tag_add:link, a.tag_add:visited, a.tag_cancel:link, a.tag_cancel:visited{color:#333333}
        a.tag_add:hover, a.tag_cancel:hover{color:#0000ff}
        .tag_list_name{float:left}
        .tag_list_operator{float:right}
    </style>
</head>
<body>
<div class="content-box"><!-- Start Content Box -->
    <div class="content-box-header">
        <h3><? echo $title ?></h3>
        <div class="clear"></div>
    </div> <!-- End .content-box-header -->
    <div class="content-box-content">
        <?
        switch ($do) {
            case "add":
                add();
                break;
            case "update":
                update($info);
                break;
            case "delete":
                delete($id);
                break;
            default:
                show_info($info);
        }
        ?>
        <? function show_box($row, $iseditmode, $isadd=0){
            global $id, $h, $url, $title, $baseurl, $option, $lang, $code_array, $tag_id, $tag_name, $tag_type, $cate_info;
            $tag_id = isset($row['tag_id']) ? $row['tag_id'] : $tag_id;
            $arr_cate=$code_array['tag_type'];
            echo $h->hidden('tag_id', $tag_id);

            $tree=unlimitedlevel($cate_info);
            $cate_array=array();
            foreach($tree as $val){
                $cate_array[$val['id']]=array(
                    'id'=>$val['id'],
                    'name'=>$val['html_prefix'].$val['name_'.$lang],
                );
            }
            echo $h->hidden('tag_type',$tag_type);
            ?>
            <table width="99%" cellspacing="0" cellpadding="3" class="mbox">
                <tr class="th"><td class="thtitle" width="120" align="right"><? echo $title ?></td><td><? if(isset($row['id'])) echo $h->hidden('id', iso($row["id"])); ?></td></tr>
                <tr><td class="l"><?=ROOT_TAGS_NAME?></td><td><input class="g" name="tag_name" value="<?echo $tag_name?>" readonly/></td></tr>
                <?if($tag_type == 1){?>
                    <tr><td class="l"><?=ROOT_CATEGORY?></td><td><? echo $h->select('category_id', 0, $cate_array, 'id', 'name', 'g', 'select_change(this)') ?></td></tr>
                <?}else{?>
                    <tr><td class="l"><?=ROOT_CATEGORY?></td><td><select name="document_id" onchange="select_change(this)">
                                <option value="1">新闻</option>
                                <option value="2">文档</option>
                    </select></td></tr>
                <?}?>
                <tr><td class="l"><?=ROOT_PRODUCT?></td><td class="product_list" style="height: 300px">123</td></tr>
                <tr><td class="l">&nbsp;</td><td><br>&nbsp;&nbsp;<a onClick="history.go(-1)">[ <?=ROOT_BACK?> ]</a><br><br></td></tr>
            </table>
        <? } ?>
        <? function show_boxes($result){
            global $url, $page, $pages, $cateid, $s, $lang, $arrlang, $filter, $h, $code_array,$tag_type, $tag_id, $tag_name, $sales_promotion_list, $promotion_type;
            $operation = '';
            $list_operation = '';
            if($tag_type==2){
                $operation = '<a href="'.$url.'.php?do=add&tag_id='.$tag_id.'">[ '.ROOT_TAGS_DOCUMENT_MANAGENT.' ]</a>';
                $list_name = ROOT_TAGS_DOCUMENT_NAME;
                $list_operation = '<a href="#">'.ROOT_TAGS_CANCLE.'</a>';
            }else{
                $operation = '<a href="'.$url.'.php?do=add&tag_id='.$tag_id.'">['.ROOT_TAGS_PRODUCT_MANAGENT.']</a>';
                $list_name = ROOT_TAGS_PRODUCT_NAME;
                $list_operation = '<a>'.ROOT_TAGS_CANCEL.'</a>';
            }
            $str='<table width="99%" cellspacing="0" class="mlist"><tr class="th"><td><strong>'.ROOT_TAGS_NAME.'</strong></td><td><strong>'.$list_name.'</strong></td><td><strong>'.ROOT_TAGS_SOURCE.'</strong></td><td>'.$operation.'</td></tr>';
            $count=1;
            foreach( $result as $row)
            {
                $id=$row['id'];
                $name = $row['list_name'];
                $discount_type = $row['discount_type'];
                $discount_value = $row['discount_value'];

                $list_operation = "<a href=\"$url.php?do=delete&tag_id=".$tag_id."&id=".$id."\">".ROOT_TAGS_CANCEL."</a>";
                $str.="<tr><td>".$tag_name."</td><td>".$name."</td><td>".$row['list_table']."</td><td>".$list_operation."</td></tr>";
                $count++;
            }
            $str.= "</table>";
            echo $str;
            if($count==1) echo "<br>No data now. (".ROOT_NO_DATA."!)";
            echo echo_page_raw($page, $pages, $url.'.php?tag_id='.$tag_id.'&page=');
        } ?>
        <br class="clear" />
    </div> <!-- End .content-box-content -->
    <br class="clear" />
</div><!-- End .content-box -->
<script type="text/javascript">
    <?
        $add_type_url = "tag_product_add.php?do=product_add";
        $cancel_type_url = "tag_product_add.php?do=product_cancel";
    ?>
    function cate_add(node){
        var parent_td = $(node).parent();
        var tag_type = $('input:hidden[name="tag_type"]').val();
        var cate_id = $('select[name="document_id"]').val();
        $.ajax({
            url: '<?=$add_type_url?>',
            data: {'p_id': parent_td.children('input:hidden').val(),
                'tag_id':"<?=$tag_id?>",
                'cate_id':cate_id,
                'tag_type':tag_type,
                'tag_list_name':$(node).children('.tag_list_name').text()
                },
            type: 'post',
            success:function(data1){
                var data_arr = eval("("+data1+")");
                var str = "<a class=\"tag_cancel\" href=\"javascript:void(0)\" onclick=\"cate_cancel(this)\"><span class=\"tag_list_name\">"+data_arr.tag_list_name+"</span><span class=\"tag_list_operator\">取消标签</span></a>";
                parent_td.append(str);
                parent_td.children('.tag_add').remove();

            }
        });
    }

    function cate_cancel(node){
        var parent_td = $(node).parent();
        $.ajax({
            url: '<?=$cancel_type_url?>',
            data: {'p_id':parent_td.children('input:hidden').val(),
                'tag_id':"<?=$tag_id?>",
                'table_name':"<?=$table_name?>",
                'tag_list_name':$(node).children('.tag_list_name').text()
                },
            type: 'post',
            success:function(datas){
                var data_arr = eval("("+datas+")");
                var str = "<a class=\"tag_add\" href=\"javascript:void(0)\" onclick=\"cate_add(this)\"><span class=\"tag_list_name\">"+data_arr.tag_list_name+"</span><span class=\"tag_list_operator\">添加标签</span></a>";
                parent_td.append(str);
                parent_td.children('.tag_cancel').remove();
            }
        });
    }

    function select_change(node){
        var node_val = $(node).val();
        var tag_type = $('input:hidden[name="tag_type"]').val();
        $.ajax({
            url: "tag_product_add.php",
            data: {'cate_id': node_val,
                'tag_type': tag_type,
                'tag_id': <?=$tag_id?>
            },
            type: 'get',
            success:function(data){
                $('.product_list').html(data);
            }
        });
    }

    $(function(){
        <?if($tag_type == 1){?>
            var select_change = $('select[name="category_id"]');
        <?}else{?>
            var select_change = $('select[name="document_id"]');
        <?}?>
        select_change.triggerHandler('change');
    });
</script>
</body>
</html>
